package com.lib.framework.helper.permission

import androidx.fragment.app.FragmentManager
import android.os.Build
import androidx.annotation.NonNull
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity


/**
 * Copyright (C), 2020-2020, 享物说
 * FileName: PermissionHelper
 * Author: keanbin
 * Date: 2020-02-05 11:03
 * Description: 权限申请辅助类
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */
class PermissionHelper {

    companion object {

        private const val TAG_PermissionFragment = "PermissionFragment"

        /**
         * 申请权限
         */
        fun requestPermissions(activity: FragmentActivity,
                               listener: OnPermissionListener, vararg permissions: String) {
            requestPermissions(activity.supportFragmentManager, listener, *permissions)
        }

        /**
         * 申请权限
         */
        fun requestPermissions(fragment: Fragment,
                               listener: OnPermissionListener, vararg permissions: String) {
            requestPermissions(fragment.childFragmentManager, listener, *permissions)
        }


        /**
         * 申请权限
         */
        fun requestPermissions(fragmentManager: FragmentManager, listener: OnPermissionListener,
                               vararg permissions: String) {
            val permissionFragment = getPermissionFragment(fragmentManager)

            // 判断是否需要申请权限
            val needRequestPermissions = mutableListOf<String>()
            for (permission in permissions) {
                if (isGranted(permissionFragment, permission)) {
                    continue
                }

                if (isRevoked(permissionFragment, permission)) {
                    continue
                }

                needRequestPermissions.add(permission)
            }

            if (needRequestPermissions.isEmpty()) {
                listener.onGranted()
            } else {
                // 申请权限
                permissionFragment.requestPermissions(needRequestPermissions.toTypedArray(), object :
                    PermissionFragment.PermissionsCallback {
                    override fun onResult(permissionResultList: List<PermissionResult>) {
                        for (permissionResult in permissionResultList) {
                            if (!permissionResult.granted) {
                                listener.onDenied()
                                return
                            }
                        }

                        listener.onGranted()
                    }
                })
            }
        }

        /**
         * 判断权限是否被授权
         */
        private fun isGranted(permissionFragment: PermissionFragment, permission: String): Boolean {
            return !isMarshMallow() || permissionFragment.isGranted(permission)
        }

        /**
         * 判断权限是否被撤回
         */
        private fun isRevoked(permissionFragment: PermissionFragment, permission: String): Boolean {
            return isMarshMallow() && permissionFragment.isRevoked(permission)
        }

        /**
         * 是否高于 M 版本
         */
        private fun isMarshMallow(): Boolean {
            return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
        }

        /**
         * 获取 PermissionFragment
         */
        private fun getPermissionFragment(@NonNull fragmentManager: FragmentManager): PermissionFragment {
            var permissionFragment = findPermissionFragment(fragmentManager)
            if (permissionFragment == null) {
                permissionFragment = PermissionFragment()
                fragmentManager.beginTransaction().add(permissionFragment, TAG_PermissionFragment)
                    .commitNow()
            }

            return permissionFragment
        }

        /**
         * 查找 PermissionFragment
         */
        private fun findPermissionFragment(@NonNull fragmentManager: FragmentManager): PermissionFragment? {
            return fragmentManager.findFragmentByTag(TAG_PermissionFragment) as? PermissionFragment
        }
    }

    /**
     * 权限申请回调
     */
    interface OnPermissionListener {

        /**
         * 授权
         */
        fun onGranted()

        /**
         * 拒绝
         */
        fun onDenied()
    }
}